﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Niknak.Services.ServiceCore.TaskScheduler;

namespace ServiceCore.Tests
{
    [TestClass]
    public class AdvancedTaskProcessorTest
    {
        [TestMethod]
        public void TestMinutelyIterations()
        {
            MockAdvancedTaskConfig config = new MockAdvancedTaskConfig()
            {
                StartDate = "2014-01-01",
                StartTime = "00:00:00",
                EndDate = "2014-01-01",
                EndTime = "00:30:00",
                IntervalType = Niknak.Services.ServiceCore.InternalEnums.TaskScheduler.IntervalType.Minutely,
                RunOnDays = Niknak.Services.ServiceCore.InternalEnums.TaskScheduler.DaysOfWeek.All
            };

            MockAdvancedTaskProcessor processor = new MockAdvancedTaskProcessor(config);

            Assert.IsTrue(processor.Iterations.Count() == 31, "Should be 31 iterations for 30 minutes, inclusive of start and end dates/times");
            Assert.IsTrue(processor.Iterations[0] == new DateTime(2014, 01, 01, 0, 0, 0), "Should be 2014-01-01 00:00:00");
            Assert.IsTrue(processor.Iterations[1] == new DateTime(2014, 01, 01, 0, 1, 0), "Should be 2014-01-01 00:01:00");
            Assert.IsTrue(processor.Iterations[2] == new DateTime(2014, 01, 01, 0, 2, 0), "Should be 2014-01-01 00:02:00");
            Assert.IsTrue(processor.Iterations[30] == new DateTime(2014, 01, 01, 0, 30, 0), "Should be 2014-01-01 00:30:00");
        }

        [TestMethod]
        public void TestHourlyIterations()
        {
            MockAdvancedTaskConfig config = new MockAdvancedTaskConfig()
            {
                StartDate = "2014-01-01",
                StartTime = "00:00:00",
                EndDate = "2014-01-02",
                EndTime = "00:00:00",
                IntervalType = Niknak.Services.ServiceCore.InternalEnums.TaskScheduler.IntervalType.Hourly,
                RunOnDays = Niknak.Services.ServiceCore.InternalEnums.TaskScheduler.DaysOfWeek.All
            };

            MockAdvancedTaskProcessor processor = new MockAdvancedTaskProcessor(config);

            Assert.IsTrue(processor.Iterations.Count() == 25, "Should be 25 iterations for 24 hours, inclusive of start and end dates/times");
            Assert.IsTrue(processor.Iterations[0] == new DateTime(2014, 01, 01, 0, 0, 0), "Should be 2014-01-01 00:00:00");
            Assert.IsTrue(processor.Iterations[1] == new DateTime(2014, 01, 01, 1, 0, 0), "Should be 2014-01-01 01:00:00");
            Assert.IsTrue(processor.Iterations[2] == new DateTime(2014, 01, 01, 2, 0, 0), "Should be 2014-01-01 02:00:00");
            Assert.IsTrue(processor.Iterations[24] == new DateTime(2014, 01, 02, 0, 0, 0), "Should be 2014-01-02 00:00:00");
        }

        [TestMethod]
        public void TestDailyIterations()
        {
            MockAdvancedTaskConfig config = new MockAdvancedTaskConfig()
            {
                StartDate = "2014-01-01",
                StartTime = "00:00:00",
                EndDate = "2014-01-31",
                EndTime = "00:00:00",
                IntervalType = Niknak.Services.ServiceCore.InternalEnums.TaskScheduler.IntervalType.Daily,
                RunOnDays = Niknak.Services.ServiceCore.InternalEnums.TaskScheduler.DaysOfWeek.All
            };

            MockAdvancedTaskProcessor processor = new MockAdvancedTaskProcessor(config);

            Assert.IsTrue(processor.Iterations.Count() == 31, "Should be 31 iterations for 30 days, inclusive of start and end dates/times");
            Assert.IsTrue(processor.Iterations[0] == new DateTime(2014, 01, 01, 0, 0, 0), "Should be 2014-01-01 00:00:00");
            Assert.IsTrue(processor.Iterations[1] == new DateTime(2014, 01, 02, 0, 0, 0), "Should be 2014-01-02 00:00:00");
            Assert.IsTrue(processor.Iterations[2] == new DateTime(2014, 01, 03, 0, 0, 0), "Should be 2014-01-03 00:00:00");
            Assert.IsTrue(processor.Iterations[30] == new DateTime(2014, 01, 31, 0, 0, 0), "Should be 2014-01-31 00:00:00");
        }

        [TestMethod]
        public void TestWeeklyIterations()
        {
            MockAdvancedTaskConfig config = new MockAdvancedTaskConfig()
            {
                StartDate = "2014-01-01",
                StartTime = "00:00:00",
                EndDate = "2014-01-29",
                EndTime = "00:00:00",
                IntervalType = Niknak.Services.ServiceCore.InternalEnums.TaskScheduler.IntervalType.Weekly,
                RunOnDays = Niknak.Services.ServiceCore.InternalEnums.TaskScheduler.DaysOfWeek.All
            };

            MockAdvancedTaskProcessor processor = new MockAdvancedTaskProcessor(config);

            Assert.IsTrue(processor.Iterations.Count() == 5, "Should be 5 iterations for 4 weeks, inclusive of start and end dates/times");
            Assert.IsTrue(processor.Iterations[0] == new DateTime(2014, 01, 01, 0, 0, 0), "Should be 2014-01-01 00:00:00");
            Assert.IsTrue(processor.Iterations[1] == new DateTime(2014, 01, 08, 0, 0, 0), "Should be 2014-01-08 00:00:00");
            Assert.IsTrue(processor.Iterations[2] == new DateTime(2014, 01, 15, 0, 0, 0), "Should be 2014-01-15 00:00:00");
            Assert.IsTrue(processor.Iterations[4] == new DateTime(2014, 01, 29, 0, 0, 0), "Should be 2014-01-30 00:00:00");
        }

        [TestMethod]
        public void TestFortnightlyIterations()
        {
            MockAdvancedTaskConfig config = new MockAdvancedTaskConfig()
            {
                StartDate = "2014-01-01",
                StartTime = "00:00:00",
                EndDate = "2014-02-26",
                EndTime = "00:00:00",
                IntervalType = Niknak.Services.ServiceCore.InternalEnums.TaskScheduler.IntervalType.Fortnightly,
                RunOnDays = Niknak.Services.ServiceCore.InternalEnums.TaskScheduler.DaysOfWeek.All
            };

            MockAdvancedTaskProcessor processor = new MockAdvancedTaskProcessor(config);

            Assert.IsTrue(processor.Iterations.Count() == 5, "Should be 5 iterations for 8 weeks, inclusive of start and end dates/times");
            Assert.IsTrue(processor.Iterations[0] == new DateTime(2014, 01, 01, 0, 0, 0), "Should be 2014-01-01 00:00:00");
            Assert.IsTrue(processor.Iterations[1] == new DateTime(2014, 01, 15, 0, 0, 0), "Should be 2014-01-15 00:00:00");
            Assert.IsTrue(processor.Iterations[2] == new DateTime(2014, 01, 29, 0, 0, 0), "Should be 2014-01-29 00:00:00");
            Assert.IsTrue(processor.Iterations[4] == new DateTime(2014, 02, 26, 0, 0, 0), "Should be 2014-02-26 00:00:00");
        }

        [TestMethod]
        public void TestMonthlyIterations()
        {
            MockAdvancedTaskConfig config = new MockAdvancedTaskConfig()
            {
                StartDate = "2014-01-01",
                StartTime = "00:00:00",
                EndDate = "2014-05-01",
                EndTime = "00:00:00",
                IntervalType = Niknak.Services.ServiceCore.InternalEnums.TaskScheduler.IntervalType.Monthly,
                RunOnDays = Niknak.Services.ServiceCore.InternalEnums.TaskScheduler.DaysOfWeek.All
            };

            MockAdvancedTaskProcessor processor = new MockAdvancedTaskProcessor(config);

            Assert.IsTrue(processor.Iterations.Count() == 5, "Should be 5 iterations for 5 months, inclusive of start and end dates/times");
            Assert.IsTrue(processor.Iterations[0] == new DateTime(2014, 01, 01, 0, 0, 0), "Should be 2014-01-01 00:00:00");
            Assert.IsTrue(processor.Iterations[1] == new DateTime(2014, 02, 01, 0, 0, 0), "Should be 2014-02-00 00:00:00");
            Assert.IsTrue(processor.Iterations[2] == new DateTime(2014, 03, 01, 0, 0, 0), "Should be 2014-03-00 00:00:00");
            Assert.IsTrue(processor.Iterations[4] == new DateTime(2014, 05, 01, 0, 0, 0), "Should be 2014-05-00 00:00:00");
        }
    }
}
